## install.packages("Matching")
## install.packages("rgenoud")

library(Matching)
library(rgenoud)
library(tidyverse)


matching <- read.csv("~/Dropbox/switchers/Matching/data/contribDB.csv")
matching <- transform(matching, id = as.numeric(factor(bonica_rid)))

matching = matching %>%
  filter(pre_total_in_district != 0 & pre_total_out_district != 0 &
           post_total_in_district != 0 & post_total_out_district != 0)

attach(matching)

## Setting the covariates to (1) match on; and (2) obtain balance on:

covariates_match <- cbind(switcher_code, dem, dwnom1, seniority, dpres, les, votepct, pre_ideo, pre_ideo_in_district, pre_ideo_out_district, pre_ideo_unknown, general_length)

covariates_balance <- cbind(switcher_code, dem, dwnom1, seniority, dpres, les, votepct, pre_ideo, pre_ideo_in_district, pre_ideo_out_district, pre_ideo_unknown, general_length)

## Performing the match -- 1-to-1 WITH replacement:

	## Finding balance -- 
match_1 <- GenMatch(Tr = switcher, X = covariates_match, BalanceMatrix = covariates_balance, M = 1, pop.size = 20000, max.generations = 10, wait.generations = 5, hard.generation.limit = FALSE, int.seed = 865, unif.seed = 225, fit.func = "pvals", exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE) 

	## Estimating the causal effect using the optimal weights 
ideo_1 <- Match(Y = post_ideo, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1)
summary(ideo_1)

in_ideo_1 <- Match(Y = post_ideo_in_district, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1, match.out = ideo_1)
summary(in_ideo_1)

out_ideo_1 <- Match(Y = post_ideo_out_district, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1, match.out = ideo_1)
summary(out_ideo_1)

unknown_ideo_1 <- Match(Y = post_ideo_unknown, Tr = switcher, X = covariates_match, M = 1, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_1, match.out = ideo_1)
summary(unknown_ideo_1)

	## Checking balance 
balance_1 <- MatchBalance(switcher ~ switcher_code + dem + dwnom1 + seniority + dpres + les + votepct + pre_ideo + pre_ideo_in_district + pre_ideo_out_district + pre_ideo_unknown + general_length, match.out = ideo_1, nboots = 5000)

	## Generating the matched dataset --
treated_1 <- matching[ideo_1$index.treated,]
treated_1$pair_id <- treated_1$id
control_1 <- matching[ideo_1$index.control,]
control_1$pair_id <- treated_1$id

matched_ideo_1 <- rbind(treated_1, control_1)
matched_ideo_1 <- matched_ideo_1[order(matched_ideo_1$pair_id),]
matched_ideo_1$match <- 1

## Performing the match -- 1-to-2 WITH replacement:

	## Finding balance -- 
match_2 <- GenMatch(Tr = switcher, X = covariates_match, BalanceMatrix = covariates_balance, M = 2, pop.size = 20000, max.generations = 10, wait.generations = 5, hard.generation.limit = FALSE, int.seed = 507, unif.seed = 642, fit.func = "pvals", exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE) 

	## Estimating the causal effect using the optimal weights 
ideo_2 <- Match(Y = post_ideo, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2)
summary(ideo_2)

in_ideo_2 <- Match(Y = post_ideo_in_district, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2, match.out = ideo_2)
summary(in_ideo_2)

out_ideo_2 <- Match(Y = post_ideo_out_district, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2, match.out = ideo_2)
summary(out_ideo_2)

unknown_ideo_2 <- Match(Y = post_ideo_unknown, Tr = switcher, X = covariates_match, M = 2, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_2, match.out = ideo_2)
summary(unknown_ideo_2)


	## Checking balance 
balance_2 <- MatchBalance(switcher ~ switcher_code + dem + dwnom1 + seniority + dpres + les + votepct + pre_ideo +  pre_ideo_in_district + pre_ideo_out_district + pre_ideo_unknown + general_length, match.out = ideo_2, nboots = 5000)

	## Generating the matched dataset --
treated_2 <- matching[ideo_2$index.treated,]
treated_2$pair_id <- treated_2$id
control_2 <- matching[ideo_2$index.control,]
control_2$pair_id <- treated_2$id

matched_ideo_2 <- rbind(treated_2, control_2)
matched_ideo_2 <- matched_ideo_2[order(matched_ideo_2$pair_id),]
matched_ideo_2 <- matched_ideo_2[!duplicated(matched_ideo_2), ]
matched_ideo_2$match <- 2

## Performing the match -- 1-to-3 WITH replacement:

	## Finding balance -- 
match_3 <- GenMatch(Tr = switcher, X = covariates_match, BalanceMatrix = covariates_balance, M = 3, pop.size = 20000, max.generations = 10, wait.generations = 5, hard.generation.limit = FALSE, int.seed = 324, unif.seed = 543, fit.func = "pvals", exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE) 

	## Estimating the causal effect using the optimal weights 
ideo_3 <- Match(Y = post_ideo, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3)
summary(ideo_3)

in_ideo_3 <- Match(Y = post_ideo_in_district, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3, match.out = ideo_3)
summary(in_ideo_3)

out_ideo_3 <- Match(Y = post_ideo_out_district, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3, match.out = ideo_3)
summary(out_ideo_3)

unknown_ideo_3 <- Match(Y = post_ideo_unknown, Tr = switcher, X = covariates_match, M = 3, exact = as.logical(c(1,1,0,0,0,0,0,0,0,0,0,0)), replace = TRUE, Weight.matrix = match_3, match.out = ideo_3)
summary(unknown_ideo_3)

	## Checking balance 
balance_3 <- MatchBalance(switcher ~ switcher_code + dem + dwnom1 + seniority + dpres + les + votepct + pre_ideo + pre_ideo_in_district + pre_ideo_out_district + pre_ideo_unknown + general_length, match.out = ideo_3, nboots = 5000)

	## Generating the matched dataset --
treated_3 <- matching[ideo_3$index.treated,]
treated_3$pair_id <- treated_3$id
control_3 <- matching[ideo_3$index.control,]
control_3$pair_id <- treated_3$id

matched_ideo_3 <- rbind(treated_3, control_3)
matched_ideo_3 <- matched_ideo_3[order(matched_ideo_3$pair_id),]
matched_ideo_3 <- matched_ideo_3[!duplicated(matched_ideo_3), ]
matched_ideo_3$match <- 3

ideo <- rbind(matched_ideo_1, matched_ideo_2, matched_ideo_3)
setwd("~/Dropbox/switchers/Matching/data/matches/APR_R&R")
write.table(ideo, "ideo.csv", sep = ",", row.names = FALSE)